<?php namespace Phpcmf\Controllers\Member;

class Quan_buy extends \Phpcmf\Member\Module
{

	// 购买圈子
	public function index() {
		if(IS_POST){
			$datas = \Phpcmf\Service::L('input')->post('data');
			$id = (int)$datas['id'];
			$data = \Phpcmf\Service::M()->table('1_quan')->get($id);
			if (!$data) {
				$this->_json(0, dr_lang('应用内容不存在'),$datas);
			}
			// 判断是否购买过
			$buy_data = \Phpcmf\Service::M()->db->table('1_quan_buy')->where('uid', $this->uid)->where('cid', $id)->countAllResults();
			if($buy_data){
				$this->_json(0, dr_lang('您已经购买过'));
			}
			//圈子设置
    		$quanzhi_shezhi_0 = \Phpcmf\Service::M()->table('1_quan_shezhi')->where('dirname', 'quan')->getRow();
			$quanzhi_shezhi = dr_string2array($quanzhi_shezhi_0['setting']);
			$quan_yongjin_0 = $quanzhi_shezhi['pages']['quan_yongjin'];
			$quan_moshi_0 = $quanzhi_shezhi['pages']['quan_moshi'];
			
			// 确保quan_yongjin是小数点形式（如0.1）且在0.1到1之间
			if($quan_moshi_0 == 1){
				$quan_yongjin_data = (int)$data['jiage'];
			} else {
				if($quan_yongjin_0){
				    // 转换为小数
					$quan_yongjin = floatval($quan_yongjin_0);
					// 格式化为两位小数（如0.1转成0.10）
					$quan_yongjin_data_0 = number_format($quan_yongjin, 2, '.', '');
					$quan_yongjin_data = $quan_yongjin_data_0 * (int)$data['jiage'];
				} else {
					// 没有佣金
					$quan_yongjin_data = (int)$data['jiage'];
				}

			}
			
			
    			// 购买
			$data_buy = [
				/*'uid' => $this->uid,
				
				'tieid' => $id,
				'jiage' => $data['jiage'],
				'tianjiashijian' => time(),
				'shenhe' => 1,*/
				'uid' => (int)$this->uid,
                'cid' => $data['id'],
                'catid' => $data['catid'],
                'quan_uid_id' => $data['uid'],
                // 'price' => $data['jiage'], //$quan_yongjin_data
				'price' => (int)$data['jiage'], // 佣金金额
                'inputtime' => SYS_TIME,
                'endtime' => SYS_TIME + (int)$data['tianshu'] * 24 * 60 * 60,
                'error' => '已经付款',
                'status' => 1,
				
			];
			\Phpcmf\Service::M()->table('1_quan_buy')->insert($data_buy);
			// 插入数据
			$insert_data = [
				'quanid' => $id,
				'uid' => $this->uid,
				'shenhe' => 1,
				'tianjiashijian' => time(),
			];
			$insert_id = \Phpcmf\Service::M()->db->table('1_quan_jiaquan')->insert($insert_data);
			//减去用户金额
			\Phpcmf\Service::M('member')->add_money($this->uid, -(int)$data['jiage']);
			// 增加交易流水记录
			\Phpcmf\Service::M('Pay')->add_paylog([
				'uid' => $this->uid,
				'username' => '',
				'touid' => $data['uid'],
				'tousername' => $data['username'],
				'mid' => 'system', // 特征字符串，system表示系统
				'title' => "购买圈子:《".$data['title']."》",
				'value' => -(int)$data['jiage'], // 交易金额，正数表示增加金额，负数表示消费金额
				'type' => 'finecms', // 支付方式，alipy，weixin，finecms表示余额
				'status' => 1, // 1表示交易成功,0表示未成功
				'result' => "购买圈子",
				'paytime' => SYS_TIME, // 付款时间，默认当前时间
				'inputtime' => SYS_TIME, // 流水参生时间，默认当前时间
			]);
			// 增加被购买用户金额
			\Phpcmf\Service::M('member')->add_money($data['uid'], $quan_yongjin_data);
			\Phpcmf\Service::M('Pay')->add_paylog([
				'uid' => $data['uid'],
				'username' => $data['username'],
				'touid' => $this->uid,
				'tousername' => '',
				'mid' => 'system', // 特征字符串，system表示系统
				'title' => "购买圈子：《".$data['title']."》",
				'value' => $quan_yongjin_data, // 交易金额，正数表示增加金额，负数表示消费金额
				'type' => 'finecms', // 支付方式，alipy，weixin，finecms表示余额
				'status' => 1, // 1表示交易成功,0表示未成功
				'result' => "购买圈子",
				'paytime' => SYS_TIME, // 付款时间，默认当前时间
				'inputtime' => SYS_TIME, // 流水参生时间，默认当前时间
			]);
			$this->_json(1, dr_lang('购买成功'),$data);
		} else {
			$this->_json(0, dr_lang('参数错误'));
		}
	}

	
}
